package com.atguigu.service.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;

import java.util.List;
import java.util.Objects;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public boolean registUser(User user) {
        if (!Objects.equals(user.getUsername(), "") && user.getPassword() != "" && user.getEmail() != "") {
            for (int i = 1; i <= userDao.getCount(); i++) {
                if (user.getUsername().equals(userDao.getUserById(i).getUsername())) {
                    return false;
                }
            }
            userDao.insert(user);
            return true;
        }
        return false;
    }

    @Override
    public boolean Login(User user) {
        if (user.getUsername() != "" && user.getPassword() != "") {
            for (int i = 1; i <= userDao.getCount(); i++) {
                if (user.getUsername().equals(userDao.getUserById(i).getUsername()) && user.getPassword().equals(userDao.getUserById(i).getPassword())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean existsUsername(String username) {
        for (int i = 1; i <= userDao.getCount(); i++) {
            if (username.equals(userDao.getUserById(i).getUsername())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Integer getLoginId(String username) {
        return userDao.getUserByName(username);
    }

    @Override
    public List<User> getUserAll() {
        return userDao.getUserAll();
    }
}
